package com.jzg.jcptadmin.adpter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jzg.jcptadmin.R;
import com.jzg.jcptadmin.app.AppContext;
import com.jzg.jcptadmin.data.vo.FastOnlineData;
import com.jzg.template.refreshrecycleview.RefreshBaseAdapter;
import com.jzg.template.refreshrecycleview.RefreshEntity;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.math.BigDecimal;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by jzg on 2015/12/25.
 */
public class YiXinAdapter extends RefreshBaseAdapter {
    private Context context;
    AppContext appContext;
    private List<FastOnlineData.AppraiseListEntity> mDatas;
    int status;

    /**
     * @param mDatas
     */
    public void updateData(List<FastOnlineData.AppraiseListEntity> mDatas) {
        this.mDatas = mDatas;
        this.notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        if (mDatas.size() < 10)
            return mDatas.size();
        return super.getItemCount();
    }

    public YiXinAdapter(Context mContext, List<FastOnlineData.AppraiseListEntity> mDatas, int position) {
        super(mContext, mDatas);
        this.context = mContext;
        appContext = (AppContext) mContext.getApplicationContext();
        this.mDatas = mDatas;
        this.status = position;
    }

    @Override
    public RecyclerView.ViewHolder getHolderView(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_fastonline, null));
    }

    @Override
    public void onBindView(RecyclerView.ViewHolder holder, int position, RefreshEntity entity) {
        MyViewHolder holder1 = (MyViewHolder) holder;
        FastOnlineData.AppraiseListEntity entity1 = (FastOnlineData.AppraiseListEntity) entity;
        if (entity1.getTaskType().equals("4")) {//线下
            showOffonlineinfo(holder1, entity1);
        } else {
            showOnlineInfo(holder1, entity1,entity1.getTaskType());
        }


    }

    private void showOffonlineinfo(MyViewHolder holder1, FastOnlineData.AppraiseListEntity entity1) {
        holder1.itemBuyCarName.setText("评估编号:" + entity1.getOrderNo());
        holder1.itemBuyCarRegion.setText("联系人: " + entity1.getContactMan());
        holder1.itemBuyCarMileage.setText("联系电话:" + entity1.getContactMobile());
        holder1.price.setText("地址:  " + entity1.getContactAddr());
        holder1.price.setTextColor(Color.parseColor("#31455f"));
        holder1.itemBuyCarSeeTimes.setText(entity1.getCreateTime());
//        ImageLoader.getInstance().displayImage("drawable://" + R.drawable.offonline_moren, holder1.itemBuyCarIcon, appContext.options);
        holder1.itemBuyCarIcon.setVisibility(View.GONE);
        if (status == 0) {
            ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pingguzhong, holder1.itemBuyCarPrice, appContext.options);
        } else {
            ImageLoader.getInstance().displayImage(entity1.getStatusImg(), holder1.itemBuyCarPrice, appContext.options);
        }

    }

    private void showOnlineInfo(MyViewHolder holder1, FastOnlineData.AppraiseListEntity entity1,String type) {
        if (type.equals("2")) {
            holder1.itemBuyCarMileage.setVisibility(View.INVISIBLE);
        }else{
            holder1.itemBuyCarMileage.setVisibility(View.VISIBLE);
            if (TextUtils.isEmpty(entity1.getBusinessPrice())) {
                holder1.itemBuyCarMileage.setText("成交价:暂无数据");
            } else {
                holder1.itemBuyCarMileage.setText("成交价:" + entity1.getBusinessPrice() + "万元");
            }
        }
        holder1.itemBuyCarName.setText("车架号:" + entity1.getVin());

        holder1.itemBuyCarRegion.setText("城市:" + entity1.getCityName());

//        String time = entity1.getCreateTime().substring(0, 10);
//        android:textColor="#FF6600"
        holder1.price.setTextColor(Color.parseColor("#FF6600"));
        if (entity1.getIsShowPrice().equals("1")) {
            try {
                String str = entity1.getAppraisePrice();
                BigDecimal bd = new BigDecimal(Double.parseDouble(str));

                holder1.price.setText("估值:" + bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "万元");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            holder1.price.setText("估值: 暂无估值");
        }
        holder1.itemBuyCarSeeTimes.setText(entity1.getCreateTime());
//        if (!entity1.getReplyCount().equals("0")) {
//            holder1.dragtext.setVisibility(View.VISIBLE);
//            holder1.dragtext.setText(entity1.getReplyCount());
//        } else {
//            holder1.dragtext.setVisibility(View.INVISIBLE);
//        }
        holder1.itemBuyCarIcon.setVisibility(View.VISIBLE);
        ImageLoader.getInstance().displayImage(entity1.getImagePath(), holder1.itemBuyCarIcon, appContext.options);
        if (status == 0) {
            ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pingguzhong, holder1.itemBuyCarPrice, appContext.options);
        } else {
            ImageLoader.getInstance().displayImage(entity1.getStatusImg(), holder1.itemBuyCarPrice, appContext.options);
        }
    }


    class MyViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.item_buy_car_icon)
        ImageView itemBuyCarIcon;
        @Bind(R.id.item_buy_car_name)
        TextView itemBuyCarName;
        @Bind(R.id.item_buy_car_mileage)
        TextView itemBuyCarMileage;
        @Bind(R.id.item_buy_car_region)
        TextView itemBuyCarRegion;
        @Bind(R.id.item_buy_car_see_times)
        TextView itemBuyCarSeeTimes;
        @Bind(R.id.item_buy_car_price)
        ImageView itemBuyCarPrice;
        //        @Bind(R.id.relativeLayout11)
//        RelativeLayout relativeLayout;
        @Bind(R.id.price)
        TextView price;
//        @Bind(R.id.dragtext)
//        DragDeleteTextView dragtext;

        public MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
